package com.tianji.learning.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domain.dto.ReplyDTO;
import com.tianji.learning.domain.po.InteractionQuestion;
import com.tianji.learning.domain.po.InteractionReply;
import com.tianji.learning.domain.po.LearningRecord;
import com.tianji.learning.domain.query.ReplyPageQuery;
import com.tianji.learning.domain.vo.QuestionVO;
import com.tianji.learning.domain.vo.ReplyVO;
import com.tianji.learning.enums.QuestionStatus;
import com.tianji.learning.mapper.InteractionQuestionMapper;
import com.tianji.learning.mapper.InteractionReplyMapper;
import com.tianji.learning.service.IInteractionReplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 互动问题的回答或评论 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-07-07
 */
@Service
public class InteractionReplyServiceImpl extends ServiceImpl<InteractionReplyMapper, InteractionReply> implements IInteractionReplyService {


    @Resource
    private InteractionQuestionMapper questionMapper;



    @Override
    public void saveReply(ReplyDTO replyDTO) {
        Long userId = UserContext.getUser();

        //1.保存回答或者评论

        InteractionReply reply = BeanUtils.copyProperties(replyDTO, InteractionReply.class);
        reply.setUserId(userId);
        save(reply);

        //2.判断是否是回答 dto.answerId == null == 回答
        InteractionQuestion question = new InteractionQuestion();
        if (replyDTO.getAnswerId() != null) {
            //3.否 累加回答下评论次数
            InteractionReply answerInfo = this.getById(replyDTO.getAnswerId());
            answerInfo.setReplyTimes(answerInfo.getReplyTimes() + 1);
            this.updateById(answerInfo);
        }else {
            //3.是 4.1修改问题表中>>最近一次回答id; 回答次数+1
             question = questionMapper.selectById(replyDTO.getQuestionId());
            question.setLatestAnswerId(reply.getId());
            question.setAnswerTimes(question.getAnswerTimes()+1);
//            questionMapper.updateById(question);
        }

        //5.是否是学生提交 isStudent == true
        if (replyDTO.getIsStudent()) {
            //5.1是>问题表中>>status
            question.setStatus(QuestionStatus.UN_CHECK);
        }

        //3.
        questionMapper.updateById(question);

    }

    @Override
    public PageDTO<ReplyVO> queryReplyPage(ReplyPageQuery query) {


        //1.获取回答或评论信息
        Page<InteractionReply> page = lambdaQuery()
                .eq(query.getQuestionId() != null, InteractionReply::getQuestionId, query.getQuestionId())
                .eq(query.getAnswerId() != null, InteractionReply::getAnswerId, query.getAnswerId())
                .eq(InteractionReply::getHidden,false)
                .page(query.toMpPage());

        List<InteractionReply> records = page.getRecords();
        if (records == null || records.isEmpty()) {
            return new PageDTO<>();
        }

        Set<Long> uids = new HashSet<>();
        Set<Long> targetReplyIds = new HashSet<>();
        for (InteractionReply record : records){
            if(!record.getAnonymity()){
                uids.add(record.getUserId());
                uids.add(record.getTargetUserId());
            }
                if(record.getTargetReplyId() != null && record.getTargetReplyId() > 0){
                        targetReplyIds.add(record.getTargetReplyId());

            }
    }
        //3.TODO  用户信息


        //x.封装到vo
        List<ReplyVO> replyVOList = new ArrayList<>();
        for (InteractionReply record : records) {
            ReplyVO replyVO = BeanUtils.copyProperties(record, ReplyVO.class);
            if (!record.getAnonymity()) {
//                replyVO.setUserName();
//                replyVO.setUserIcon();
            }

//            replyVO.setTargetUserName();

            replyVOList.add(replyVO);
        }
        return PageDTO.of(page,replyVOList);
    }
}
